package com.tensquare.search.controller;


import com.tensquare.search.pojo.Article;
import com.tensquare.search.service.ArticleService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("article")
@CrossOrigin
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    //1.添加文章
    @PostMapping
    public Result add(@RequestBody Article article){
        //1.添加到索引库
        articleService.add(article);
        //2.返回结果
        return new Result(true,StatusCode.OK,"添加文章成功！");
    }
    //2.根据标题或文章内容进行搜索
    @GetMapping("search/{keywords}/{page}/{size}")
    public Result search(@PathVariable String keywords,@PathVariable int page,@PathVariable int size){
        //1.返回查询到的分页结果
        PageResult<Article> pageResult=articleService.search(keywords,page,size);
        //2.返回结果
        return new Result(true,StatusCode.OK,"条件查询成功！",pageResult);
    }

}
